﻿import { ElMessageBox } from 'element-plus';
import request from '/@/utils/request';

enum Api {
	AddStkreview = '/api/stkreview/add',
	DeleteStkreview = '/api/stkreview/delete',
	UpdateStkreview = '/api/stkreview/update',
	PageStkreview = '/api/stkreview/page',
	DetailStkreview = '/api/stkreview/detail',
	GetApprovalHistoryList = '/api/stkreview/getApprovalHistoryList',
	DeleteFilesByRelation = '/api/sysFile/deleteByRelation',
}

// 增加叠构审核
export const addStkreview = (params?: any) =>
	request({
		url: Api.AddStkreview,
		method: 'post',
		data: params,
	});

// Add interface for file deletion
export interface DeleteFilesByRelationInput {
	relationName: string;
	appSource: string;
	relationId?: number | null;
}

// Add the new API function for deleting related files
export const deleteFilesByRelation = (params: DeleteFilesByRelationInput) =>
	request({
		url: Api.DeleteFilesByRelation,
		method: 'post',
		data: params,
	});

// 删除叠构审核
export const deleteStkreview = async (params?: any) => {
	// First delete the stkreview record
	//ElMessageBox.alert(params.job_name + ':' + params.AppSource);
	if (!params.job_name) {
		throw new Error('删除条件不足：需要提供job_name');
	}

	// 删除主记录
	await request({
		url: Api.DeleteStkreview,
		method: 'post',
		data: params,
	});

	// Then delete associated files
	await deleteFilesByRelation({
		relationName: params.job_name,
		appSource: '叠层审核',
		relationId: 0, // 0 means delete all files related to this job name
	});
};

// 编辑叠构审核
export const updateStkreview = (params?: any) =>
	request({
		url: Api.UpdateStkreview,
		method: 'post',
		data: params,
	});

// 分页查询叠构审核
export const pageStkreview = (params?: any) =>
	request({
		url: Api.PageStkreview,
		method: 'post',
		data: params,
	});

// 详情叠构审核
export const detailStkreview = (params: any) =>
	request({
		url: Api.DetailStkreview,
		method: 'get',
		params: params, // 改用 params 而不是 data，因为是 GET 请求
	});

// 详情叠构审核
export const getApprovalHistoryList = (params: any) =>
	request({
		url: Api.GetApprovalHistoryList,
		method: 'get',
		params: params, // 改用 params 而不是 data，因为是 GET 请求
	});

export const getAttachments = (params: { jobName: string; revisionId: string; type: 'submit' | 'approve' }) =>
	request({
		url: '/api/stkreview/getAttachments',
		method: 'get',
		params: params,
	});

export const uploadAttachment = (formData: FormData) =>
	request({
		url: '/api/stkreview/uploadAttachment',
		method: 'post',
		data: formData,
		headers: {
			'Content-Type': 'multipart/form-data',
		},
	});

export const deleteAttachment = (params: { jobName: string; revisionId: string; fileName: string; type: 'submit' | 'approve' }) =>
	request({
		url: '/api/stkreview/deleteAttachment',
		method: 'post',
		data: params,
	});

interface DownloadResponse {
	data: Blob;
}

export const downloadAttachmentFile = (params: { path: string }): Promise<DownloadResponse> =>
	request({
		url: '/api/stkreview/downloadFile',
		method: 'get',
		params: params,
		responseType: 'blob',
	});

// 发送审核结果邮件，匹配后端 EmailSendService 的接口定义
export interface EmailAttachment {
	fileName: any;
	fileContent: string;
	contentType?: string;
}

export interface EmailSendRequest {
	subject: string;
	htmlContent: string;
	toEmails: string[];
	ccEmails?: string[];
	attachments?: EmailAttachment[];
	emailFromName?: string;
}

// 发送邮件接口
export const sendEnhancedEmail = (params: EmailSendRequest) =>
	request({
		url: 'api/emailSend/sendEnhancedEmail',
		method: 'post',
		data: params,
	});
export const exportToExcel = () =>
	request({
		url: '/api/stkreview/exportToExcel',
		method: 'get',
		responseType: 'blob',
	});
